package com.sharp.schedule.dao.repository;

import com.github.pagehelper.PageInfo;
import com.sharp.kernel.constant.CommonConst;
import com.sharp.kernel.constant.db.SqlConstant;
import com.sharp.kernel.mybatis.repository.BaseMysqlRepository;
import com.sharp.schedule.dao.entity.SaSchedule;
import com.sharp.schedule.dao.mapper.SaScheduleDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Title: SaScheduleService
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2020/10/16 16:53
 * @description Project Name: Seed
 * @Package: com.srct.service.dao.repository
 */
@Service
public class SaScheduleService extends BaseMysqlRepository<SaSchedule, SaScheduleDao> {

    @Resource
    private SaScheduleDao saScheduleDao;

    public List<SaSchedule> getAllScheduleForInit() {
        Example example = super.getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonConst.YES);
        criteria.andEqualTo(SqlConstant.VALID_FIELD, SqlConstant.VALID);
        return saScheduleDao.selectByExample(example);
    }

    public void disableByJobName(String jobName) {
        Example example = super.getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("jobName", jobName);
        super.disable(example);
    }

    public SaSchedule getSchedule(String type, String service, String function, String parameter) {
        Example example = super.getExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", type);
        criteria.andEqualTo("service", service);
        criteria.andEqualTo("function", function);
        if (StringUtils.isNotBlank(parameter)) {
            criteria.andEqualTo("parameter", parameter);
        } else {
            criteria.andIsNull("parameter");
        }
        criteria.andEqualTo("status", CommonConst.YES);
        criteria.andEqualTo("valid", CommonConst.YES);
        return super.selectOne(example);
    }

    public PageInfo<SaSchedule> querySchedule(String type, String temporary, Date startAt, Date endAt, int currentPage, int pageSize) {
        Example example = super.getExample();
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(type)) {
            criteria.andEqualTo("type", type);
        }
        if (StringUtils.isNotBlank(temporary)) {
            criteria.andEqualTo("temporary", temporary);
        }
        if (startAt != null) {
            criteria.andGreaterThanOrEqualTo("startTime", startAt);
        }
        if (endAt != null) {
            criteria.andLessThanOrEqualTo("startTime", endAt);
        }
        criteria.andEqualTo("valid", CommonConst.YES);
        example.orderBy("createAt").desc();
        return super.select(example, currentPage, pageSize);
    }
}



